package network;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {
//    private Socket socket = null;
//
//    public TCPClient(String ip, int port) throws IOException {
//        // 客户端在创建 Socket 对象时, 就会和服务器建立 TCP 连接.
//        // 创建 Socket 对象时, 会自动为该对象分配一个本地的 IP 地址和端口号.
//        socket = new Socket(ip, port);
//    }
//
//    public void start() throws IOException {
//        System.out.println("client start!");
//
//        try (InputStream inputStream = socket.getInputStream();
//             OutputStream outputStream = socket.getOutputStream()) {
//
//            Scanner scanner = new Scanner(System.in);
//            Scanner scannerNetwork = new Scanner(inputStream);
//            PrintWriter writer = new PrintWriter(outputStream);
//
//            while (true) {
//                // 1. 从控制台读取数据.
//                System.out.print("-> ");
//                String request = scanner.next();
//                // 2. 把用户输入的字符串, 转换成字节数组, 发送给服务器.
//                writer.println(request);
//                writer.flush();
//                // 3. 接收服务器返回的数据.
//                if (!scannerNetwork.hasNext()) {
//                    break;
//                }
//                String response = scannerNetwork.next();
//                // 4. 把字节数组转换成字符串, 输出到控制台.
//                System.out.println(response);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void main(String[] args) throws IOException {
//        TCPClient client = new TCPClient("10.64.79.73",9090);
//        client.start();
//    }


    private Socket socket = null;

    public TCPClient(String serverIp, int serverPort) throws IOException {
        // 客户端在 new Socket 的时候, 就会和服务器建立 TCP 连接.
        // 此时少了服务器 IP 和 端口.
        socket = new Socket(serverIp, serverPort);
        // socket.connect(new InetSocketAddress(InetAddress.getByName(serverIp), serverPort));
    }

    public void start() {
        System.out.println("client start!");


        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()) {
            Scanner scanner = new Scanner(System.in);

            Scanner scannerNetwork = new Scanner(inputStream);
            PrintWriter writer = new PrintWriter(outputStream);

            while (true) {
                // 1. 从控制台读取用户输入.
                System.out.print("-> ");
                String request = scanner.next();
                // 2. 把请求发送给服务器.
                writer.println(request);
                writer.flush();
                // 3. 从服务器读取响应
                if (!scannerNetwork.hasNext()) {
                    break;
                }
                String response = scannerNetwork.next();
                // 4. 把响应显示到控制台上
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TCPClient client = new TCPClient("127.0.0.1", 9090);
        client.start();
    }
}
